package com.googlecode.keepwalking;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.googlecode.keepwalking.WalkingDetector.WalkingDetectorBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class KeepWalking extends Activity {
	private static final String TAG = "KeepWalking";
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	private WalkingDetector detector;
	private boolean fBound = false;
	private Thread thread;
	private TextView tvTimestamp;
	private TextView tvWalking;
	private TextView tvStart;
	private TextView tvAcceleration;
	private TextView tvStdDev;
	
	private ServiceConnection sc = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(TAG, "onServiceConnected");
			detector = ((WalkingDetectorBinder)service).getService();
			fBound = true;
			startDetector();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d(TAG, "onServiceDisconnected");
			fBound = false;
		}
	};
	
	private OnClickListener cl = new OnClickListener() {
		public void onClick(View v) {
			switch(v.getId()) {
			case R.id.bStart:
				startDetectorService();
				break;
			case R.id.bStop:
				stopDetectorService();
				break;
			case R.id.bLog:
				startLogActivity();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	
	@Override
	public void onStart() {
		Log.d(TAG, "onStart");
		super.onStart();
		tvTimestamp = (TextView)findViewById(R.id.tvTimestamp);
		tvWalking = (TextView)findViewById(R.id.tvWalking);
		tvStart = (TextView)findViewById(R.id.tvStart);
		tvAcceleration = (TextView)findViewById(R.id.tvAcceleration);
		tvStdDev = (TextView)findViewById(R.id.tvStdDev);
		Button bLog = (Button)findViewById(R.id.bLog);
		bLog.setOnClickListener(cl);
		Button bStart = (Button)findViewById(R.id.bStart);
		bStart.setOnClickListener(cl);
		Button bStop = (Button)findViewById(R.id.bStop);
		bStop.setOnClickListener(cl);
		Intent intent = new Intent(this, WalkingDetector.class);
		bindService(intent, sc, BIND_AUTO_CREATE);
	}
	
	@Override
	public void onStop() {
		Log.d(TAG, "onStop");
		super.onStop();
		fBound = false;
		unbindService(sc);
	}
	
	private void startDetectorService() {
		Intent service = new Intent(this, WalkingDetector.class);
		service.setAction(WalkingDetector.ACTION_START);
		this.startService(service);
	}
	
	private void stopDetectorService() {
		Intent service = new Intent(this, WalkingDetector.class);
		service.setAction(WalkingDetector.ACTION_STOP);
		this.stopService(service);
	}
	
	public synchronized void startDetector() {
		if(fBound) {
			detector.startDetect();
			thread = new Thread() {
				public void run() {
					while(fBound) {
						WalkingStatus status = detector.getStatus();
						updateGUI(status);
						try {
							Thread.sleep(10000);
						} catch(InterruptedException e) {
						}
					}
				}
			};
			thread.start();
		} else {
			Toast.makeText(this, "Walking Detector service not connected", Toast.LENGTH_LONG).show();
		}
	}
	
	public synchronized void stopDetector() {
		if(fBound) {
			detector.stopDetect();
		} else {
			Toast.makeText(this, "Walking Detector service not connected", Toast.LENGTH_LONG).show();
		}
	}
	
	public void startLogActivity() {
		Intent intent = new Intent(this, LogActivity.class);
		startActivity(intent);
	}
	
	private void updateGUI(final WalkingStatus status) {
		//Log.d(TAG, "updateGUI");
		runOnUiThread(new Runnable() {
			public void run() {
				tvTimestamp.setText(format.format(new Date(status.timestamp)));
				tvWalking.setText("Walking: " + status.fWalking);
				if(status.fWalking) {
					tvStart.setText("Walking starts at: " + format.format(new Date(status.starttime)));
					tvAcceleration.setText("Acc: " + status.acceleration);
					tvStdDev.setText("StdDev: " + status.stddev);
				} else {
					tvStart.setText("Walking starts at: " + "N/A");
					tvAcceleration.setText("Acc: " + "N/A");
					tvStdDev.setText("StdDev: " + "N/A");
				}
			}
		});
	}
}
